package com.linkgie.galaxyframework.crypto.context;

import java.io.InputStream;
import java.io.OutputStream;

import com.linkgie.galaxyframework.crypto.AsymmetricStreamingCipherService;
import com.linkgie.galaxyframework.crypto.PrivateKey;
import com.linkgie.galaxyframework.crypto.PublicKey;
import com.linkgie.galaxyframework.crypto.SignatureService;
import com.linkgie.galaxyframework.crypto.spi.AsymmetricFuncSPI;
import com.linkgie.galaxyframework.crypto.spi.AsymmetricStreamingCipherFuncSPI;
import com.linkgie.galaxyframework.crypto.spi.PrivateKeybytes;
import com.linkgie.galaxyframework.crypto.spi.PublicKeybytes;

/**
 * 同时支持签名和加密的非对称密码算法的实现；
 * 
 * @author huanghaiquan
 *
 */
public class AsymmetricStreamingCipherSignatureServiceImpl extends AsymmetricCipherSignatureServiceImpl
		implements AsymmetricStreamingCipherService, SignatureService {

	private final AsymmetricStreamingCipherFuncSPI STREAMING_CIPHER_SPI;
	public AsymmetricStreamingCipherSignatureServiceImpl(AsymmetricFuncSPI innerService) {
		super(innerService);
		STREAMING_CIPHER_SPI = (AsymmetricStreamingCipherFuncSPI) innerService;
	}

	@Override
	public int encrypt(PublicKey key, InputStream in, OutputStream out) {
		PublicKeybytes pubKeyBytes = ENCODING.decode(key);
		return STREAMING_CIPHER_SPI.encrypt(pubKeyBytes, in, out);
	}

	@Override
	public int encrypt(byte[] rawKey, InputStream in, OutputStream out) {
		return STREAMING_CIPHER_SPI.encrypt(rawKey, in, out);
	}

	@Override
	public int decrypt(PrivateKey key, InputStream in, OutputStream out) {
		PrivateKeybytes privKeyBytes = ENCODING.decode(key);
		return STREAMING_CIPHER_SPI.decrypt(privKeyBytes, in, out);
	}

	@Override
	public int decrypt(byte[] rawKey, InputStream in, OutputStream out) {
		return STREAMING_CIPHER_SPI.decrypt(rawKey, in, out);
	}
	
}
